package com.lczyfy.idcp.modules.sys.vueweb;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lczyfy.idcp.common.config.Global;
import com.lczyfy.idcp.common.utils.ResponseCode;
import com.lczyfy.idcp.common.utils.ServerResponse;
import com.lczyfy.idcp.common.utils.StringUtils;
import com.lczyfy.idcp.common.web.BaseController;
import com.lczyfy.idcp.modules.sys.entity.Menu;
import com.lczyfy.idcp.modules.sys.service.SystemService;
import com.lczyfy.idcp.modules.sys.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Api(value = "menu")
@RestController
@RequestMapping(value = "${apiPath}/sys/menu")
public class MenuVueController extends BaseController {
    @Autowired
    private SystemService systemService;
    @ApiOperation(value = "获取菜单列表",notes = "")
    @RequiresPermissions("sys:menu:view")
    @RequestMapping(value = "search",method = RequestMethod.GET) //菜单列表
    public ServerResponse<Menu> search() {
        List<Menu> list = Lists.newArrayList();
        List<Menu> sourcelist = systemService.findAllMenu();
        Menu.sortList(list, sourcelist, Menu.getRootId(), true);
        return new ServerResponse<Menu>(ResponseCode.SUCCESS.getCode(),list);
    }
    @ApiOperation(value = "添加和更新菜单列表",notes ="添加菜单")
    @RequiresPermissions("sys:menu:edit")
    @RequestMapping(value = "save",method = RequestMethod.POST) //菜单列表
    public ServerResponse<String> create(@RequestBody Menu menu) {
        if(!UserUtils.getUser().isAdmin()){
            return ServerResponse.createByErrorMessage("越权操作，只有超级管理员才能添加或修改数据！");
        }
        if(Global.isDemoMode()){
            return ServerResponse.createByErrorMessage("演示模式，不允许操作！");
        }
        Menu menuParent = systemService.getMenu(menu.getMenuParentId());
        menu.setParent(menuParent);
        systemService.saveMenu(menu);
        return ServerResponse.createBySuccessMessage("保存菜单成功");
    }
    @ApiOperation(value = "删除菜单列表",notes ="删除菜单")
    @RequiresPermissions("sys:menu:edit")
    @RequestMapping(value = "delete",method = RequestMethod.DELETE) //菜单列表
    public ServerResponse<String> delete(@RequestBody Menu menu) {
        if(!UserUtils.getUser().isAdmin()){
            return ServerResponse.createByErrorMessage("越权操作，只有超级管理员才能添加或修改数据！");
        }
        if(Global.isDemoMode()){
            return ServerResponse.createByErrorMessage("演示模式，不允许操作！");
        }
        systemService.deleteMenu(menu);
        return ServerResponse.deleteBySuccessMessage("删除菜单成功");
    }
    @ApiOperation(value = "通过菜单详情",notes = "获取详情")
    @RequiresPermissions("sys:menu:view")
    @RequestMapping(value = "findById/{id}",method = RequestMethod.GET) //菜单列表
    public ServerResponse<Menu> findById(@PathVariable String id) {
        Menu menu = systemService.getMenu(id);
        if (menu.getParent()==null||menu.getParent().getId()==null){
            menu.setParent(new Menu(Menu.getRootId()));
        }
        menu.setParent(systemService.getMenu(menu.getParent().getId()));
        menu.setMenuParentId(menu.getParent().getId());
        // 获取排序号，最末节点排序号+30
        if (StringUtils.isBlank(menu.getId())){
            List<Menu> list = Lists.newArrayList();
            List<Menu> sourcelist = systemService.findAllMenu();
            Menu.sortList(list, sourcelist, menu.getParentId(), false);
            if (list.size() > 0){
                menu.setSort(list.get(list.size()-1).getSort() + 30);
            }
        }
        return  new ServerResponse(ResponseCode.SUCCESS.getCode(),menu) ;
    }
    /**
     * 批量修改菜单排序
     */
    @ApiOperation(value = "排序列表",notes ="批量修改菜单排序")
    @RequiresPermissions("sys:menu:edit")
    @RequestMapping(value = "updateSort",method = RequestMethod.POST) //菜单列表
    public ServerResponse<String> updateSort(@RequestBody String[] ids, Integer[] sorts) {
        if(Global.isDemoMode()){
            return  ServerResponse.createByErrorMessage("演示模式，不允许操作！");
        }
        for (int i = 0; i < ids.length; i++) {
            Menu menu = new Menu(ids[i]);
            menu.setSort(sorts[i]);
            systemService.updateMenuSort(menu);
        }
        return  ServerResponse.createBySuccessMessage("保存菜单排序成功!");
    }
    @RequiresPermissions("user")
    @ResponseBody
    @RequestMapping(value = "treeData", method = RequestMethod.GET)
    @ApiOperation(value = "获取菜单组织机构")
    public ServerResponse<List<Map<String, Object>>>treeData(@RequestParam(required=false) String extId, @RequestParam(required=false) String isShowHide, HttpServletResponse response) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        List<Menu> list = systemService.findAllMenu();
        for (int i=0; i<list.size(); i++){
            Menu e = list.get(i);
            if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
                if(isShowHide != null && isShowHide.equals("0") && e.getIsShow().equals("0")){
                    continue;
                }
                Map<String, Object> map = Maps.newHashMap();
                map.put("id", e.getId());
                map.put("pId", e.getParentId());
                map.put("name", e.getName());
                mapList.add(map);
            }
        }
        return  new ServerResponse(ResponseCode.SUCCESS.getCode(), mapList);
    }
}
